#include<iostream>
#include <ostream>
#include <string>
#include"httplib.h"
#include "add_contact.pb.h"
using namespace std;
using namespace httplib;
const string ip = "127.0.0.1";
const int port = 8083;

void menu()
{
    cout <<"-------------------------------------------------" << endl;
    cout << "--------------请选择对应通讯录操作----------------" << endl;
    cout << "-----------------1.新增联系人--------------------" << endl;
    cout << "-----------------2.删除联系人--------------------" << endl;
    cout << "-----------------3.查看联系人列表--------------------" << endl;
    cout << "-----------------4.查看联系人详细信息--------------------" << endl;
    cout << "-----------------0.退出--------------------" << endl;
    cout << "------------------------------------------------" << endl;

}

void buildAddReq(add_contact::AddContactRequest& req)
{
   cout << "------------------新增联系人--------------------" << endl;
   string name;
   cout << "请输入联系人姓名: ";
   getline(cin,name);
   req.set_name(name);

   cout << "请输入联系人年龄: ";
   int age;
   cin >> age;
   req.set_age(age);
   cin.ignore(256,'\n');

   for(int i = 0 ; ;i++)
   {
     cout << "请输入联系人电话: " << i+1 << "(只输入回车完成电话新增): " ;
     string number;
     getline(cin,number);
     if(number.empty()) break;
     add_contact::AddContactRequest_Phone* phone = req.add_phone();
     phone->set_number(number);
     cout << "请输入该电话类型(1. 移动电话 2. 固定电话): ";
     int type;
     cin >> type;
     cin.ignore(256,'\n');
     switch (type) 
     {
       case 1:
          phone->set_type(add_contact::AddContactRequest_Phone_PhontType::AddContactRequest_Phone_PhontType_MP);
          break;
       case 2:
          phone->set_type(add_contact::AddContactRequest_Phone_PhontType::AddContactRequest_Phone_PhontType_TEL);
          break;
       default:
          cout << "输入错误!" << endl;
          break;    
     }
   }

}

void addContact()
{
    //1.构造req
    add_contact::AddContactRequest req;
    buildAddReq(req);

    //2.序列化req
    string req_str;
    if(!req.SerializeToString(&req_str))
    {
         cout << "序列化失败" << endl;
         return;
    }
    //3.发送post请求
    Client cli(ip.c_str(),port);
    auto result = cli.Post("/contact_add",req_str,"application/protobuf");
    if(!result)
    {
        cout << "/contact_add连接失败,错误信息: " << to_string(result.error()) << endl;
        return;
    }
    //4.反序列化
    add_contact::AddContactResponse resp;
    bool parse = resp.ParseFromString(result->body);
    if(!parse)
    {
        cout << "/contact-add 反序列化失败 " << to_string(result->status) << " (" << result->reason << ")" << endl;
        return;
    }
    else if(result->status != 200)
    {
        cout <<"/contact_add 调用失败 " << to_string(result->status) << " (" << result->reason << ")" << " 错误原因:" 
             << resp.err_desc() << endl;
        return;      
    }
    else if(!resp.success())
    {
        cout <<"/contact_add 结果异常:" << resp.err_desc() << endl;
        return;
    }
    //成功
    cout << "新增联系人成功,联系人uid:" << resp.uid() << endl;
}

int main()
{
    enum OPTION
    {
        QUIT = 0,
        ADD,
        DEL,
        FIND_ALL,
        FIND_ONE
    };   
   while(true)
   {
     menu();
     cout << "--->请选择:" ;
     int opt;
     cin >> opt;
     cin.ignore(256,'\n');
     switch(opt)
     {
        case OPTION::QUIT:
            cout << "--->程序退出..." << endl;
            return 0;
        case OPTION::ADD:
            addContact();
            break;
        case OPTION::DEL:
        case OPTION::FIND_ALL:
        case OPTION::FIND_ONE:
             break;
        default:
              cout << "选择错误,请重新选择.." << endl;
              break;                
     }
   }



    return 0;
}